package 数论.约数个数;

import java.util.Scanner;

public class Hankson的趣味题 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t -- > 0){
            long a, b, c, d, ret = 0;
            a = sc.nextInt();
            b = sc.nextInt();
            c = sc.nextInt();
            d = sc.nextInt();

            for(long i = 1; i * i <= d; ++ i ) {
                if(d % i == 0) {
                    long p = d / i;
                    if(lcm(c, i) == d && gcd(a, i) == b) {//讨论的是i
                        ret++;
                    }
                    if(p == i)continue;
                    if(lcm(c, p) == d && gcd(a, p) == b) {
                        ret++;
                    }
                }
            }
            System.out.println(ret);
        }
    }
    public static long gcd(long n, long m) {
        return m != 0? gcd(m, n % m) : n;
    }
    public static long lcm(long a, long b) {
        return a * b / gcd(a, b);
    }

}
